package com.ruoyi.bill.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.bill.domain.bo.BillPortBo;
import com.ruoyi.bill.domain.vo.BillFinancePortVo;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.dataMain.listener.BillCustomInvestListener;
import com.ruoyi.dataMain.listener.BillfinancePortImportListener;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.bill.domain.vo.BillCustomInvestVo;
import com.ruoyi.bill.domain.bo.BillCustomInvestBo;
import com.ruoyi.bill.service.IBillCustomInvestService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户台账
 *
 * @author linziheng
 * @date 2024-02-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bill/customInvest")
public class BillCustomInvestController extends BaseController {

    private final IBillCustomInvestService iBillCustomInvestService;
    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "开户导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<BillCustomInvestVo> result = ExcelUtil.importExcel(file.getInputStream(), BillCustomInvestVo.class, new BillCustomInvestListener());
        return R.ok(result.getAnalysis());
    }


    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "客户台账", BillCustomInvestVo.class, response);
    }
    /**
     * 查询客户台账列表
     */
    @SaCheckPermission("bill:customInvest:list")
    @GetMapping("/list")
    public TableDataInfo<BillCustomInvestVo> list(BillCustomInvestBo bo, PageQuery pageQuery) {
        return iBillCustomInvestService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户台账列表
     */
    @SaCheckPermission("bill:customInvest:export")
    @Log(title = "客户台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BillCustomInvestBo bo, HttpServletResponse response) {
        List<BillCustomInvestVo> list = iBillCustomInvestService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户台账", BillCustomInvestVo.class, response);
    }

    /**
     * 获取客户台账详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("bill:customInvest:query")
    @GetMapping("/{id}")
    public R<BillCustomInvestVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBillCustomInvestService.queryById(id));
    }


    /**
     * 补录客户台账
     */
    @SaCheckPermission("bill:customInvest:add")
    @Log(title = "端口台账", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/record")
    public R<Void> record(@Validated(AddGroup.class) @RequestBody BillCustomInvestBo bo) {
        return toAjax(iBillCustomInvestService.recordByBo(bo));
    }

    /**
     * 新增客户台账
     */
    @SaCheckPermission("bill:customInvest:add")
    @Log(title = "客户台账", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BillCustomInvestBo bo) {
        return toAjax(iBillCustomInvestService.insertByBo(bo));
    }

    /**
     * 修改客户台账
     */
    @SaCheckPermission("bill:customInvest:edit")
    @Log(title = "客户台账", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BillCustomInvestBo bo) {
        bo.setCheckType("1");// 字典值   custom_check_type
        if(iBillCustomInvestService.deleteWithValidByIds(Collections.singletonList(bo.getId()), true)){
            return toAjax(iBillCustomInvestService.recordByBo(bo));
        }else{
          return R.fail("修改失败");
        }
    }

    /**
     * 删除客户台账
     *
     * @param ids 主键串
     */
    @SaCheckPermission("bill:customInvest:remove")
    @Log(title = "客户台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBillCustomInvestService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
